import React from 'react';
import {
  View,
  StyleSheet,
  Image,
  Text,
  BackHandler,
  SafeAreaView
} from 'react-native';
import {
  iconFailure,
  iconBigSuccessGreen
} from '@/images';
import { msg, XMHeader, Button, BackImageOn } from '@/common';
import {
  px2dp,
  fontColorBlack,
  fontColorSecDeepGray,
  isAndroid,
  mainBgColorWhite,
  splitLineColorLightGray,
} from '@/styles';

const { Submit } = Button;

export default class InsuranceFeedBack extends React.Component {
  // static navigationOptions = ({navigation}) => ({
  //   headerLeft: (
  //     <TouchableOpacity
  //       activeOpacity={0.8}
  //       style={isAndroid ? {marginLeft: 14} : {}}
  //       onPress={() => {
  //         msg.emit('router: refreshRoutes', {
  //           routeNames: ['InsuranceTab', 'InsuranceApplyListMy'],
  //         });
  //         msg.emit('router: goToNext', {
  //           routeName: 'InsuranceApplyListMy',
  //           routerFrom: 'InsuranceApplyForm',
  //         });
  //       }}>
  //       <BackImage />
  //     </TouchableOpacity>
  //   ),
  //   headerTitle: '提交结果',
  //   headerStyle: {
  //     borderBottomWidth: 0,
  //     elevation: 0,
  //   },
  // });
  constructor(props) {
    super(props);
    this.state = {
      headerTitle: '',
      status: '',
      title: '',
      desc: '',
      routerFrom: '',
      id: '', // 申请id
    };
  }

  componentDidMount() {
    if (isAndroid) {
      this.listener = BackHandler.addEventListener(
        'hardwareBackPress',
        this.onBack,
      );
    }

    const { navigation } = this.props;
    const state = navigation.state || {};
    const params = state.params || {};
    const { headerTitle, status, title, desc, routerFrom, id } = params;
    this.setState({ headerTitle, status, title, desc, routerFrom, id });
  }

  componentWillUnmount() {
    this.listener && this.listener.remove();
  }

  render() {
    const { headerTitle, status, title, desc } = this.state;
    return (
      <SafeAreaView style={{ flex: 1 }}>
        <View style={styles.container}>
          <XMHeader
            title={headerTitle}
            renderLeft={() => {
              return null;
            }}
          />
          <Image
            source={status ? iconBigSuccessGreen : iconFailure}
            resizeMode='stretch'
            style={styles.img}
          />
          <Text style={styles.title}>{title}</Text>
          <Text style={styles.desc}>{desc}</Text>

          {this._renderBtn()}
        </View>
      </SafeAreaView>
    );
  }

  _renderBtn = () => {
    const { status, routerFrom, id } = this.state;
    if (routerFrom == 'InsuranceApplyForm') {
      return (
        <View style={styles.btnWrap}>
          {!!status && (
            <Submit
              text='查看报价申请'
              boxStyle={{
                width: px2dp(520),
                height: px2dp(80),
                borderRadius: px2dp(12),
                overflow: 'hidden',
              }}
              btnStyle={{}}
              btnTextStyle={{
                fontSize: px2dp(32),
              }}
              onClick={() => {
                msg.emit('router: refreshRoutes', {
                  routeNames: ['InsuranceTab', 'InsuranceApplyListMy'],
                });
                msg.emit('router: goToNext', {
                  routeName: 'InsuranceApplyMy',
                  routerFrom: 'InsuranceApplyForm',
                  id
                });
              }}
            />
          )}
          <Submit
            text='返回报价首页'
            boxStyle={{
              width: px2dp(520),
              height: px2dp(80),
              borderRadius: px2dp(12),
              overflow: 'hidden',
              marginTop: px2dp(40),
              backgroundColor: mainBgColorWhite,
              borderWidth: 1,
              borderColor: splitLineColorLightGray,
            }}
            btnStyle={{}}
            btnTextStyle={{
              fontSize: px2dp(32),
              color: fontColorBlack,
            }}
            onClick={() => {
              msg.emit('router: refreshRoutes', {
                routeNames: ['InsuranceTab', 'InsuranceApplyListMy'],
              });
              msg.emit('router: goToNext', {
                routeName: 'InsuranceApply',
              });
            }}
          />
        </View>
      );
    }
    if (routerFrom == 'InsuranceUpload') {
      return (
        <View style={styles.btnWrap}>
          <Submit
            text='返回订单'
            boxStyle={{
              width: px2dp(520),
              height: px2dp(80),
              borderRadius: px2dp(12),
              overflow: 'hidden',
              marginTop: px2dp(40),
              backgroundColor: mainBgColorWhite,
              borderWidth: 1,
              borderColor: splitLineColorLightGray,
            }}
            btnStyle={{}}
            btnTextStyle={{
              fontSize: px2dp(32),
              color: fontColorBlack,
            }}
            onClick={() => {
              msg.emit('router: refreshRoute', {
                routeName: 'InsuranceApplyListMy',
              });
              msg.emit('router: goToNext', {
                routeName: 'InsuranceApplyListMy',
                routerFrom: 'InsuranceUpload'
              });
            }}
          />
        </View>
      );
    }
  };

  onBack = () => {
    return true;
  };
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    backgroundColor: mainBgColorWhite,
  },
  img: {
    width: px2dp(300),
    height: px2dp(300),
    marginTop: px2dp(35),
  },
  title: {
    fontSize: px2dp(36),
    color: fontColorBlack,
    fontWeight: '800',
  },
  desc: {
    fontSize: px2dp(28),
    marginTop: px2dp(12),
    color: fontColorSecDeepGray,
    width: px2dp(560),
    textAlign: 'center',
  },
  btnWrap: {
    position: 'absolute',
    bottom: px2dp(100),
  },
});
